﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class RegisteredCoursesEN : System.Web.UI.Page
{
    public Persons mobjPersons
    {
        get
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjPersons"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mobjPersons"].ToString()] = value;
        }
    }

    public List<Courses> mlstCourses
    {
        get
        {
            if (ViewState["mlstCourses"] == null)
                ViewState["mlstCourses"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mlstCourses"].ToString()] as List<Courses>;
        }
        set
        {
            if (ViewState["mlstCourses"] == null)
                ViewState["mlstCourses"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mlstCourses"].ToString()] = value ;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (!Page.IsPostBack)
        {

            

            checkSecurityAndCustomizePage();

            bindddlLanguage();
            showLanguage();

            bindGrid();

        }
    }

    /// <summary>
    /// this function will get if the language filter will shown or not
    /// </summary>
    private void showLanguage()
    {
        SystemOptionsDB objDB = new SystemOptionsDB(ConnectionStrings.LMS);
        List<SystemOptions> lst = objDB.Select("", "");

        if (lst.Count < 1)
        {
            dvShowLanguage.Visible = true;
            grdCourses.Columns[3].Visible = true;
        }
        else
        {

            dvShowLanguage.Visible = lst[0].ShowCourseLanguage;
            grdCourses.Columns[3].Visible = lst[0].ShowCourseLanguage;
        }


        if (lst.Count < 2)
        {

            grdCourses.Columns[2].Visible = true;
        }
        else
        {


            grdCourses.Columns[2].Visible = lst[1].ShowCourseLanguage;
        }

    }

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGrid();
    }

    /// <summary>
    /// this function will bind the drop down list of languages
    /// </summary>
    private void bindddlLanguage()
    {
        LanguagesDB objDB = new LanguagesDB(ConnectionStrings.LMS);
        List<Languages> lst = objDB.Select("", "");
        lst = (from obj in lst orderby obj.LanguageNameEN select obj).ToList<Languages>();

        lst.Insert(0, new Languages { ID = -1, LanguageNameAR = "-- الكل --", LanguageNameEN = "-- All --" });

        ddlLanguage.DataSource = lst;
        ddlLanguage.DataTextField = "LanguageNameEN";
        ddlLanguage.DataValueField = "ID";
        ddlLanguage.DataBind();
        ddlLanguage.SelectedValue = "-1";

    }


    /// <summary>
    /// this function will bind the grid with the registered courses
    /// </summary>
    private void bindGrid()
    {
        Persons currentPerson = GlobalVars.getCurrentPerson();
        

        mlstCourses = currentPerson.lstCourses;

        if (ddlLanguage.SelectedValue != "-1")
        {
            long language = long.Parse(ddlLanguage.SelectedValue);
            mlstCourses = (from obj in mlstCourses where obj.LanguageID == language select obj).ToList<Courses>();
        }

        grdCourses.DataSource = mlstCourses;
        grdCourses.DataBind();
    }

    protected void grdCourses_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Persons currentPerson = GlobalVars.getCurrentPerson();
            Courses objCourses = e.Row.DataItem as Courses;



            if (objCourses != null)
            {

                Label lblSpendTime = (Label)e.Row.FindControl("lblSpendTime");
                Panel pnlStart = (Panel)e.Row.FindControl("pnlStart");
                Label lblCourseStatus = (Label)e.Row.FindControl("lblStatus");



                if (objCourses.isCaptivate)
                {

                    GlobalVars.CustomizeGrid(objCourses, currentPerson, lblSpendTime, pnlStart, lblCourseStatus,false);


                }

                else
                {


                    GlobalVars.CustomizeGridNotCaptivate(objCourses, currentPerson, lblSpendTime, pnlStart, lblCourseStatus,false);
                    
                }

            }
        }
    }


    /// <summary>
    /// this function will check the user previliges and  customize page
    /// </summary>
    protected void checkSecurityAndCustomizePage()
    {
        Persons currentPerson = GlobalVars.getCurrentPerson();
        mobjPersons = currentPerson;
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Registered_courses_page))
        {
            Response.Redirect("loginEN.aspx");
            return;
        }


        
        

    }




    protected void grdCourses_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCourses.DataSource = mlstCourses;
        grdCourses.PageIndex = e.NewPageIndex;
        grdCourses.DataBind();
        

    }


    protected void grdCourses_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "RefreshCourse")
        {
            long corId = long.Parse(e.CommandArgument.ToString());
            long regId = 0;

            foreach (RegisteredCourses obj in mobjPersons.lstRegisteredCourses)
            {
                if (obj.CoursesID == corId)
                {
                    regId = obj.ID;
                    break;
                }
            }


            RegisteredCoursesDB objRegisteredCoursesDB = new RegisteredCoursesDB(ConnectionStrings.LMS);



            List<RegisteredCourses> lstRegisteredCourses = objRegisteredCoursesDB.getRegisteredCoursesByID(regId);

            if (lstRegisteredCourses.Count > 0)
            {
                GlobalVars.refreshRegistered(lstRegisteredCourses[0]);
            }

            grdCourses.DataSource = mlstCourses;

            grdCourses.DataBind();

        }

    }
}